﻿using ED.Common.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace ED.FreeSql
{
    public interface IHeaderExt
    {
        /// <summary>
        /// 来源操作编码
        /// </summary>
        string OriginElementCode { get; }

    }

    public class HeaderExt : IHeaderExt
    {
        private readonly IHttpContextAccessor accessor;

        /// <summary>
        /// 登录用户构造函数
        /// </summary>
        /// <param name="_accessor"></param>
        public HeaderExt(IHttpContextAccessor _accessor)
        {
            accessor = _accessor;

        }

        /// <summary>
        /// 来源操作编码
        /// </summary>
        public virtual string OriginElementCode
        {
            get
            {
                var tmp = "";
                StringValues un = "";
                var existInHeader = accessor?.HttpContext?.Request?.Headers?.TryGetValue("bcoc", out un);
                if (existInHeader == true)
                {
                    var undecode = WebUtility.UrlDecode(un[0]);
                    tmp = undecode.To<string>();
                }
                return tmp;
            }
        }
    }
}
